package cn.donghuapian.service.impl;

import cn.donghuapian.entity.ImageEntity;
import cn.donghuapian.mapper.ImageMapper;
import cn.donghuapian.service.IImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzw
 * @since 2024-09-14
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> implements IImageService {

    @Override
    public void add(MultipartFile file, String name) {
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setName(name);
        try {
            file.getSize();
            imageEntity.setData(file.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        save(imageEntity);
    }

    @Override
    public void update(MultipartFile file, String name, int id) {
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setName(name); 
        imageEntity.setId(id);
        try {
            imageEntity.setData(file.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        updateById(imageEntity);
    }
}
